/**
 * Created by admin on 2017/11/24.
 */
$(function () {
    //图片列表上传
    $(".product_upload_img").click(function () {
        $(this).next().click();
    });
    //图片列表上传
    $(".imgs").change(function(event) {
        event.preventDefault();
        var n=event.target.files.length;
        var file;
        var field = $(this).attr('data-field');
        for (var i = 0; i < n; i++) {
            file=event.target.files[i];
            product_img_upload(file,field,1);
        }
    });
    //单图上传
    $(".img").change(function(event) {
        event.preventDefault();
        var n=event.target.files.length;
        var file;
        var field = $(this).attr('data-field');
        for (var i = 0; i < n; i++) {
            file=event.target.files[i];
            product_img_upload(file,field);
        }
    });
    //商家图片列表上传
    $(".vue_imgs").change(function(event) {
        event.preventDefault();
        var n=event.target.files.length;
        var file;
        var field = $(this).attr('data-field');
        for (var i = 0; i < n; i++) {
            file=event.target.files[i];
            vue_img_upload(file,field,1);
        }
    });
    //商家单图上传
    $(".vue_img").change(function(event) {
        event.preventDefault();
        var n=event.target.files.length;
        var file;
        var field = $(this).attr('data-field');
        for (var i = 0; i < n; i++) {
            file=event.target.files[i];
            vue_img_upload(file,field);
        }
    });

})
//产品图片上传
function product_img_upload(file,field,is_multi = 0){
    var form_data=new FormData();
    form_data.append("image",file);
    $.ajax({
        url: '/admin/upload/ajax_upload_img',
        type: 'POST',
        processData: false,
        contentType: false,
        data: form_data,
        dataType:"json",
        success:function (res) {
            // res = JSON.parse(res);
            if(res.code==200){
                if(is_multi == 1){
                    put_imgs(res.data.img_url,field);
                }else {
                    put_img(res.data.img_url,field);
                }
            }else {
                layer.msg(res.msg);
            }
        }
    })
}
//供应商资料上传
function vue_img_upload(file,field,is_multi = 0){
    var form_data=new FormData();
    form_data.append("image",file);
    layer.load(1);
    $.ajax({
        url: '/admin/upload/ajax_upload_img',
        type: 'POST',
        processData: false,
        contentType: false,
        data: form_data,
        dataType:"json",
        success:function (res) {
            // res = JSON.parse(res);
            layer.closeAll('loading');
            if(res.code==200){
                if(is_multi == 1){
                    data[field].push(res.data.img_url)
                }else {
                    data[field] = res.data.img_url
                }
            }else {
                layer.msg(res.msg);
            }
        }
    })
}

//添加图片
function put_img(url,field){
    $("#"+field).empty();
    var img = '<div class="img_box col-xs-12 col-md-3"> ' +
        '<a class="fancybox" href="'+url+'"> ' +
        '<img alt="image" src="'+url+'" /> ' +
        '<input type="hidden" name="'+field+'" value="'+url+'"> ' +
        '</a> ' +
        '<a class="glyphicon glyphicon-trash" onclick="del_img(this);"></a> ' +
        '</div>';
    $("#"+field).append(img);
}

//添加图片列表
function put_imgs(url,field){
    var img = '<div class="img_box col-xs-12 col-md-3"> ' +
        '<a class="fancybox" href="'+url+'"> ' +
        '<img alt="image" src="'+url+'" /> ' +
        '<input type="hidden" name="'+field+'[]" value="'+url+'"> ' +
        '</a> ' +
        '<a class="glyphicon glyphicon-trash" onclick="del_img(this);"></a> ' +
        '</div>';
    $("#"+field).append(img);
}

//删除图片
function del_img(a) {
    layer.confirm('确定删除吗?', {
        btn: ['确定','取消'] //按钮
    }, function(){
        $(a).parent().remove();
        layer.closeAll();
    }, function(){
    });
}

//删除图片
function del_wx_img(a,field) {
    let img = $(a).attr('data-value');
    layer.confirm('确定删除吗?', {
        btn: ['确定','取消'] //按钮
    }, function(){
        if(typeof (data[field]) == 'string'){
            data[field] = "";
        }else {
            $.each(data[field],function (k,v) {
                if(v == img){
                    data[field].splice(k,1)
                }
            })
        }

        layer.closeAll();
    }, function(){
    });
}

//富文本图片上传
function sendFile(file, editor, $editable){

    var filename = false;
    try{
        filename = file['name'];
    } catch(e){
        filename = false;
    }
    if(!filename){
        $(".note-alarm").remove();
    }

    //以上防止在图片在编辑器内拖拽引发第二次上传导致的提示错误
    var formdata = new FormData();
    formdata.append("image", file);
    formdata.append("key",filename); //唯一性参数

    $.ajax({
        data: formdata,
        type: "POST",
        url: "/admin/upload/ajax_upload_img",
        cache: false,
        dataType:'json',
        contentType: false,
        processData: false,
        success: function(res) {

            if(res.code ==200){

                editor.insertImage($editable, res.data.img_url);
            }else {
                layer.msg(res.msg);

            }
        }

    });
}